<template>
  <div>
    <div class="form">
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="试卷管理" name="1">
          <div class="search">
            <div class="date_picker">
              <el-input
                placeholder="课程"
                v-model="testPaperName"
                class="search_info"
              >
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
              </el-input>
              <el-input
                placeholder="名称"
                v-model="courseName"
                class="search_info"
              >
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
              </el-input>
              <el-select v-model="isHomeShow" placeholder="是否展示在首页">
                <el-option
                  v-for="item in showList"
                  :key="item.key"
                  :label="item.value"
                  :value="item.key"
                ></el-option>
              </el-select>
            </div>
            <div>
              <el-button type="primary" @click="getList()" class="searchBtn"
                >搜索</el-button
              >
              <el-button @click="add()" class="searchBtn">添加试卷</el-button>
            </div>
          </div>
          <el-table
            class="listTable"
            height="600px"
            v-loading="listLoading"
            :data="list"
            border
            fit
            highlight-current-row
            style="width: 100%"
          >
            <el-table-column label="名称" prop="id" width="100" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.testPaperName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="课程">
              <template slot-scope="{ row }">
                <span>{{ row.courseName }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="首页展示"
              prop="isHomeShow"
              width="100"
              align="center"
            >
              <template slot-scope="{ row }">
                <span v-if="row.isHomeShow == '0'">否</span>
                <span v-if="row.isHomeShow == '1'">是</span>
              </template>
            </el-table-column>
            <el-table-column label="类型" width="160" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.testPaperType }}</span>
              </template>
            </el-table-column>
            <el-table-column label="试卷级别" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.classType | level }}</span>
              </template>
            </el-table-column>
            <el-table-column label="考试时长（分钟）" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.testPaperTime }}</span>
              </template>
            </el-table-column>
            <el-table-column label="试卷总分" width="100" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.testPaperScore }}</span>
              </template>
            </el-table-column>
            <el-table-column label="及格分数" width="100" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.passScore }}</span>
              </template>
            </el-table-column>
            <el-table-column label="状态" width="100" align="center">
              <template slot-scope="{ row }">
                <span v-if="row.status == 0">正常</span>
                <span v-else>停用</span>
              </template>
            </el-table-column>
            <el-table-column label="组卷方式" width="100" align="center">
              <template slot-scope="{ row }">
                <span v-if="row.testPaperMethod == 0">随机</span>
                <span v-else>人工</span>
              </template>
            </el-table-column>
            <!-- <el-table-column label="组卷时间" width="100" align="center">
          <template slot-scope="{row}">
            <span>{{ row.passScore }}</span>
          </template>
        </el-table-column> -->
            <el-table-column label="答题次数" width="100" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.answerTimes }}</span>
              </template>
            </el-table-column>
            <el-table-column label="教员试卷" width="100" align="center">
              <template slot-scope="{ row }">
                <span v-if="row.isTeacherTestPaper == 0">是</span>
                <span v-else>非</span>
              </template>
            </el-table-column>
            <el-table-column label="PC答题状态" width="100" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.pcUpDownFlag | status }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="排序"
              prop="orderNum"
              width="80"
              align="center"
            >
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-info"
                  @click="preview(scope.row.id)"
                  v-hasPermi="['financeProduct:product:edit']"
                  >预览</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  v-hasPermi="['financeProduct:product:remove']"
                  @click="edit(scope.row.id)"
                  >编辑</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="testDelete(scope.row.id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :total="total"
            layout="total, prev, pager, next"
            :page-size="limit"
            :page-sizes="[10, 20, 50, 100]"
            @size-change="handleSizeChange"
            :current-page.sync="curPage"
            @current-change="getList()"
            class="sortStyle"
          />
        </el-tab-pane>
        <el-tab-pane label="试卷类型" name="2">
          <div class="addBtn">
            <el-button type="primary" @click="addType">新增</el-button>
          </div>

          <el-table
            class="listTable"
            :data="typeList"
            border
            fit
            highlight-current-row
            style="width: 100%"
          >
            <el-table-column label="编号" prop="id" width="100" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.id }}</span>
              </template>
            </el-table-column>
            <el-table-column label="试卷类型名称" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.testPaperType }}</span>
              </template>
            </el-table-column>
            <el-table-column label="子标题分层" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.multiLevelFlag | flag }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  v-hasPermi="['financeProduct:product:remove']"
                  @click="editType(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  v-if="scope.row.id > 5"
                  @click="handleDelete(scope.row.id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :total="totalType"
            layout="total, prev, pager, next"
            :page-size="limit"
            :page-sizes="[10, 20, 50, 100]"
            @size-change="handleSizeChange"
            :current-page.sync="curPage"
            @current-change="getTypeList($event)"
            class="sortStyle"
          />
        </el-tab-pane>
      </el-tabs>
    </div>
    <el-dialog :visible.sync="creatShow" title="试卷类型" width="666px">
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="类型名称" prop="testPaperType">
          <el-input v-model="testPaperType"  style="width: 300px"  placeholder="请输入类型名称" />
        </el-form-item>
        <el-form-item label="子标题分层" prop="testPaperType">
          <el-select
            v-model="multiLevelFlag"
            placeholder="请选择"
            style=" margin-bottom: 16px;width: 300px"
          >
            <el-option
              v-for="item in falgList"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="backBtn">
        <el-button @click="creatShow = false">取消</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPreview,
  getTestList,
  testTypeDelete,
  getTypeList,
  addTypeList,
  testDelete,
  editTypeList,
} from "@/api/system/user";
import {} from "@/api/article";
export default {
  components: {},
  filters: {
    status: function (val) {
      switch (val) {
        case "0":
          return "已下架";
        case "1":
          return "已上架";
        default:
          return "已下架";
      }
    },
    flag: function (val) {
      switch (val) {
        case "0":
          return "否";
        case "1":
          return "是";
      }
    },
    level: function (val) {
      switch (val) {
        case "0":
          return "消防设施操作员基础知识";
        case "1":
          return "消防设施操作员初级";
        case "4":
          return "消防设施操作员中级（监控方向）";
        case "5":
          return "消防设施操作员高级（监控方向）";
        case "6":
          return "消防设施操作员中级（维保方向）";
        case "7":
          return "消防设施操作员高级（维保方向）";
      }
    },
  },
  data() {
    return {
      multiLevelFlag: "",
      falgList: [
        { id: 0, value: "否" },
        { id: 1, value: "是" },
      ],
      form: {},
      testPaperType: "",
      typeList: [],
      activeName: "1",
      list: [],
      listLoading: false,
      total: 0,
      totalType: 0,
      limit: 10,
      curPage: 1,
      listData: [],
      name: "",
      className: "",
      creatShow: false,
      testPaperName: "",
      courseName: "",
      isHomeShow: "",
      showList: [
        { key: "1", value: "是" },
        { key: "0", value: "否" },
      ],
      id: null,
    };
  },
  created() {},
  mounted() {
    this.getList();
    this.getTypeList();
  },
  methods: {
    editType(val) {
      this.creatShow = true;
      this.testPaperType = val.testPaperType;
      this.id = val.id;
    },
    submit() {
      let params = {
        testPaperType: this.testPaperType,
        multiLevelFlag: this.multiLevelFlag
      };
      if (this.id) {
        params.id = this.id;
        editTypeList(params).then((res) => {
          if (res.code == 200) {
            this.$message({ message: res.msg, type: "success" });
            this.creatShow = false;
            this.getTypeList();
            // this.total = res.total;
          } else {
            this.$message({ message: res.msg, type: "error" });
          }
        });
      } else {
        addTypeList(params).then((res) => {
          if (res.code == 200) {
            this.$message({ message: res.msg, type: "success" });
            this.creatShow = false;
            this.getTypeList();
          } else {
            this.$message({ message: res.msg, type: "error" });
          }
        });
      }
    },
    addType() {
      this.id = null;
      this.creatShow = true;
    },
    getTypeList() {
      let params = {
        pageNum: this.curPage,
        pageSize: this.limit,
      };
      getTypeList(params).then((res) => {
        if (res.code == 200) {
          this.typeList = res.rows;
          this.totalType = res.total;
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    /** 查询用户列表 */
    getList() {
      let params = {
        pageNum: this.curPage,
        pageSize: this.limit,
        testPaperName: this.testPaperName,
        courseName: this.courseName,
        isHomeShow: this.isHomeShow,
      };
      this.listLoading = true;
      getTestList(params).then((res) => {
        this.listLoading = false;
        if (res.code == 200) {
          this.list = res.rows;
          this.total = res.total;
          this.list.forEach((element) => {
            if (element.testPaperType == "1") {
              element.testPaperType = "每日作业";
            } else if (element.testPaperType == "2") {
              element.testPaperType = "押题模考";
            } else if (element.testPaperType == "3") {
              element.testPaperType = "结业考试";
            } else if (element.testPaperType == "4") {
              element.testPaperType = "学前测试";
            } else if (element.testPaperType == "5") {
              element.testPaperType = "日常练习";
            }
          });
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    preview(id) {
      this.$router.push({ path: "/preview", query: { id: id } });
    },
    edit(id) {
      this.$router.push({ path: "/testEdit", query: { id: id } });
    },
    testDelete(id) {
      this.$confirm("是否确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info",
      })
        .then(() => {
          testDelete(id).then((res) => {
            if (res.code == "200") {
              this.getList();
              this.$message({ message: res.msg, type: "success" });
            } else {
              this.$message({ message: res.msg, type: "error" });
            }
          });
        })
        .catch(() => {});
    },
    handleDelete(id) {
      this.$confirm("是否确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info",
      })
        .then(() => {
          testTypeDelete(id).then((res) => {
            if (res.code == "200") {
              this.getTypeList(1);
              this.$message({ message: res.msg, type: "success" });
            } else {
              this.$message({ message: res.msg, type: "error" });
            }
          });
        })
        .catch(() => {});
    },
    handleSizeChange() {},
    add() {
      this.$router.push("/testAdd");
    },
  },
};
</script>
<style scoped>
.form {
  padding-left: 26px;
  padding-right: 24px;
  padding-top: 24px;
  background: #fff;
}
.search {
  display: flex;
  position: relative;
}
.search_info {
  width: 192px;
  height: 40px;
  margin-right: 16px;
}
.searchBtn {
  margin-left: 16px;
  height: 36px;
}
.sortStyle {
  height: 19px;
  float: right;
  /* right: 24px; */
  margin-top: 20px;
  margin-bottom: 30px;
}
.pos_right_btn {
  position: absolute;
  right: 0;
}
.opt_font {
  cursor: pointer;
  color: #2e82ff;
}
.detail_title {
  font-size: 16px;
  font-weight: 400;
  color: #212121;
  line-height: 22px;
}
.detail_title span {
  padding-left: 4px;
  background: #1e88e5;
  margin-right: 12px;
}
.row {
  display: flex;
  padding-bottom: 12px;
}
.detail_info {
  padding-top: 16px;
}
.info_left {
  display: flex;
}
.info_right {
  display: flex;
  padding-left: 202px;
}
.info_left_title {
  font-size: 14px;
  font-weight: 400;
  color: #616161;
  line-height: 20px;
  width: 112px;
  margin-right: 16px;
}
.info_left_content {
  font-size: 14px;
  font-weight: 400;
  color: #212121;
  line-height: 20px;
  width: 150px;
}
.pos_top {
  padding-top: 12px;
}
.detailTable {
  margin-top: 16px;
}
.opt_font {
  color: #1e88e5;
  cursor: pointer;
}
.page_pos {
  text-align: right;
  padding-top: 10px;
}
.backBtn {
  text-align: center;
  margin-top: 10px;
}
/deep/ .el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-height: calc(100% -30px);
  /* max-width: ; */
}
/deep/ .el-dialog .el-dialog__body {
  flex: 1;
  height: 700px;
  overflow-y: auto;
  padding-top: 0;
}
.tab {
  display: flex;
  position: absolute;
  right: 32px;
  top: 14px;
}
.tab_border {
  border: 1px solid #409eff;
  color: #409eff;
  padding: 10px 12px;
  font-size: 14px;
  margin-right: 16px;
}
.change {
  border: 1px solid #f56c6c;
  color: #f56c6c;
}
.listTable {
  margin-top: 17px;
}
.addBtn {
  float: right;
  margin-bottom: 16px;
}
</style>
